package au.edu.anu.club.cs.ft;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


public class FTClient {
	public static final String REQUEST_URL = "https://www.google.com/fusiontables/api/query";
	private String table = "1ZIO1FeFylWyzwKVa2Pgzz4Ac9kxWQf42tWQs2RU";
	private String username, password, token;
	
	public FTClient() {
		this.token = "";
	}
	
	public FTClient(String username, String password) {
		this.username = username;
		this.password = password;
		// TODO: Authenticate and get token.
		this.token = "";
	}
	
	public String getUpdates() {
		String result = "";
		String q = "SELECT TeamID, Latitude, Longitude, Time FROM " + table + "";
		result = this.query(q);
		return result;
	}
	
	public String query(String query) {
	    Map<String, String> headers = null;
	    //if (!this.token.isEmpty()) {
	    //	headers = new HashMap<String, String>();
	    //	headers.put("Authorization", "GoogleLogin auth=" + this.token);
	    //}
	    
		String result = "";
		String lower = query.toLowerCase();
	    query = "sql=" + URLEncoder.encode(query);
		
		if (lower.startsWith("select")) {
			result = RequestHandler.sendHttpRequest(FTClient.REQUEST_URL + "?" + query, "GET", null, headers);
		}
		return result;
	}
}
